package map.android.baidu.carpool.hybrid;

import java.lang.ref.WeakReference;
import java.net.URLDecoder;

import map.android.baidu.carpool.utils.ToastForCarpool;

import android.app.Activity;
import android.webkit.GeolocationPermissions;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

import com.baidu.components.api.extra.APIProxy;

public class BaseWebChromeClient extends WebChromeClient {

	protected WeakReference<Activity> mActivityRef;

	public BaseWebChromeClient(Activity activity) {
		this.mActivityRef = new WeakReference<Activity>(activity);
	}

	/**
	 * 开启Html5地理位置定位
	 */
	@Override
	public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
		callback.invoke(origin, true, false);
		super.onGeolocationPermissionsShowPrompt(origin, callback);
	}

	@Override
	public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
		if (result == null)
			return true;
		result.cancel();
		ToastForCarpool.show(APIProxy.res().getBaseActivity(), message);
		return true;
	}

	/**
	 * 拦截js 的 prompt方法
	 */
	@Override
	public boolean onJsPrompt(WebView view, String url, String message, String value, JsPromptResult result) {
		try {
			URLDecoder.decode(message, "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		result.cancel();
		return true;
	}
}